import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit


class MainWindow(QWidget):#继承QWidget
    def __init__(self, *args, **kwargs):#self is default parameter;you can add other parameters;
        #*args is mean any parameters; *kwargs is mean any dictronary parameters;
        super().__init__()#继承执行父类的构造函数
        self.setWindowTitle("lineedit组件") #set window title;be overrides the title
        ###############################################
        self.resize(400,300)
        layout = QVBoxLayout()
        self.lineedit1=QLineEdit()
        self.lineedit1.cursorPositionChanged.connect(self.cursorPositionChanged)#光标事件
        self.lineedit1.editingFinished.connect(self.editingFinished)#输入完成事件 鼠标离开父元素才算完成;回车也算完成
        self.lineedit1.selectionChanged.connect(self.selectionChanged)#光标点进去就触发
        self.lineedit1.textChanged.connect(self.textChanged)#文本改变事件
        self.lineedit1.textEdited.connect(self.textEdited)#文本编辑事件,与textChanged不同，textChanged是文本改变时触发，textEdited是文本编辑时触发
        self.lineedit2=QLineEdit()
        self.lineedit2.setEchoMode(QLineEdit.EchoMode.Password)#密码模式
        self.lineedit3=QLineEdit()
        self.lineedit3.setEchoMode(QLineEdit.EchoMode.PasswordEchoOnEdit)#密码模式，输入时显示
        self.lineedit4=QLineEdit()
        self.lineedit4.setEchoMode(QLineEdit.EchoMode.NoEcho)#不显示
        layout.addWidget(self.lineedit1)
        layout.addWidget(self.lineedit2)
        layout.addWidget(self.lineedit3)
        layout.addWidget(self.lineedit4)
        self.setLayout(layout)
        ###############################################
        bg_color = kwargs.get('setbackgroundColor', None)
        if bg_color:
            self.setStyleSheet(f"background-color: {bg_color};")
        self.show()
    def cursorPositionChanged(self, old_pos, new_pos):#光标事件
        print(old_pos, new_pos)
    def editingFinished(self):#输入完成事件
        print("over")
    def selectionChanged(self):#光标点进去就触发
        print("selectionChanged")
    def textChanged(self, text):#文本改变事件
        print(self.lineedit1.text())
    def textEdited(self, text):
        print(text)

if __name__ == "__main__":
    #print(sys.argv) #['D:\\u\\test\\gitee\\pythonqt\\test1\\改造程序\\new_hello2.py'] print sys.argv;default is this file path.
    app=QApplication(sys.argv)#get parameters from command line.
    w=MainWindow(setbackgroundColor="lightblue") #use MainWindow created a window.
    w.setWindowTitle("lineedit组件")
    w.show()#显示窗口
    #app.exec()#运行程序

    sys.exit(app.exec()) #app.exec() is first stop,then out process

    #import time #test app end,but process is not stop.
    #for i in range(10):#genarate a dictionary
    #    print(i)
    #    time.sleep(1)
# app=QApplication([])
# window=QWidget(windowTitle="Hello World",)#创建一个窗口
# window.show()#显示窗口
# app.exec()#运行程序
